package com.maidada.codesharebackend.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.maidada.codesharebackend.enums.UserRoleEnum;
import com.maidada.codesharebackend.model.entity.User;
import com.maidada.codesharebackend.service.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
 * @author wulinxuan
 * @date 2025-10-22 10:00
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Resource
    private UserService userService;

    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        return Collections.emptyList();
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return Optional.ofNullable(loginId)
                .map(Object::toString)
                .map(Long::parseLong)
                .map(id -> {
                    User user = userService.getById(id);
                    return UserRoleEnum.fromCode(user.getRole());
                })
                .map(UserRoleEnum::getDescription)
                .map(Collections::singletonList)
                .orElse(Collections.emptyList());
    }
}
